package com.yao.longhan.presenter.holder;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yao.longhan.BaseApplication;
import com.yao.longhan.R;
import com.yao.longhan.net.download.DownloadInfo;
import com.yao.longhan.net.download.DownloadManager;
import com.yao.longhan.presenter.BaseHolder;
import com.yao.longhan.utils.ToastUtils;

import butterknife.BindView;

/**
 * Created by Qiao on 2017/8/7.
 */

public class DownloadHolder extends BaseHolder<DownloadInfo> {
    public DownloadHolder(View itemView) {
        super(itemView);
    }

    @BindView(R.id.item_download_cover_iv)
    ImageView item_download_cover_iv;
    @BindView(R.id.item_download_name_tv)
    TextView item_download_name_tv;
    @BindView(R.id.item_download_author_tv)
    TextView item_download_author_tv;
    @BindView(R.id.item_download_category_tv)
    TextView item_download_category_tv;
    @BindView(R.id.item_download_progress_pb)
    ProgressBar item_download_progress_pb;
    @BindView(R.id.item_download_button_iv)
    ImageView item_download_button_iv;
    @BindView(R.id.item_download_state_tv)
    TextView item_download_state_tv;

    @Override
    public void bindData(int position, DownloadInfo downloadInfo) {
        item_download_name_tv.setText(downloadInfo.getName());
        item_download_author_tv.setText(downloadInfo.getAuthor());
        item_download_category_tv.setText(String.format("进度：%s", downloadInfo.getStatus()));
        item_download_progress_pb.setProgress((int) (downloadInfo.getCurrent() * 100.0 / downloadInfo.getTotal()));
        setStateUI(downloadInfo.getState());
        final DownloadInfo map = DownloadManager.map(downloadInfo);
        map.setOnDownloadListener(new DownloadInfo.DownloadListener() {
            @Override
            public void onDownloadProgressChanged(long current, long total) {
                int progress = (int) (current * 100.0 / total);
                item_download_progress_pb.setProgress(progress);
                item_download_state_tv.setText(String.format("下载中%s%%", progress));
                item_download_button_iv.setImageResource(R.mipmap.download_pause);
            }

            @Override
            public void onDownloadStateChanged(int state) {
                setStateUI(state);
            }
        });
        item_download_button_iv.setOnClickListener(view -> {
            switch (map.getState()) {
                case DownloadInfo.NONE:
                case DownloadInfo.PAUSE:
                case DownloadInfo.FAILED:
                    DownloadManager.download(map);
                    break;
                case DownloadInfo.WAITING:
                case DownloadInfo.DOWNLOADING:
                    DownloadManager.pause(map);
                    break;
                case DownloadInfo.FINISH:
                    open(downloadInfo);
                    break;
            }
        });
    }

    private void setStateUI(int state) {
        switch (state) {
            case DownloadInfo.NONE:
                item_download_state_tv.setText("下载");
                item_download_button_iv.setImageResource(R.mipmap.download_start);
                break;
            case DownloadInfo.DOWNLOADING:
                item_download_state_tv.setText("连接中...");
                item_download_button_iv.setImageResource(R.mipmap.download_pause);
                break;
            case DownloadInfo.PAUSE:
                item_download_state_tv.setText("继续");
                item_download_button_iv.setImageResource(R.mipmap.download_start);
                break;
            case DownloadInfo.FINISH:
                item_download_state_tv.setText("已完成");
                item_download_progress_pb.setProgress(100);
                item_download_button_iv.setImageResource(R.mipmap.download_ok);
                break;
            case DownloadInfo.WAITING:
                item_download_state_tv.setText("等待中");
                item_download_button_iv.setImageResource(R.mipmap.download_pause);
                break;
            case DownloadInfo.FAILED:
                item_download_state_tv.setText("失败");
                item_download_button_iv.setImageResource(R.mipmap.download_error);
                break;
        }
    }

    private void open(DownloadInfo downloadInfo) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            uri = FileProvider.getUriForFile(BaseApplication.getInstance(), BaseApplication.getInstance().getPackageName(), downloadInfo.getFile());
        } else {
            uri = Uri.fromFile(downloadInfo.getFile());
        }
        intent.setDataAndType(uri, "text/plain");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            BaseApplication.getInstance().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.showToast("无法打开的类型");
        }
    }
}
